package org.openit.portail.web;

import java.util.Date;

import org.openit.portail.dao.ParametreServiceDao;
import org.openit.portail.model.Panier;
import org.openit.portail.model.ParametreService;
import org.openit.portail.model.Service;
import org.openit.portail.service.ServiceService;
import org.openit.portail.service.UtilService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping(ServiceController.URL)
public class ServiceController {

	static final String URL = "/service";

	@Autowired
	private ServiceService serviceService;
	@Autowired
	private ParametreServiceDao parametreServiceDao;
	@Autowired
	private UtilService utilService;
	@ModelAttribute("panier")
	public Panier getPanier(){
		return utilService.getPanier();
	}

	@RequestMapping(method = RequestMethod.GET)
	public String getService(ModelMap model) {
		model.put("produits", serviceService.getServices());
        return "service";
	}

	@Secured("SUPER_ADMIN,ADMIN")
	@RequestMapping(method = RequestMethod.GET, value = "newservice")
	public String getFormService(ModelMap model) {
		Service service = new Service();

		model.addAttribute("service", service);
		return "formservice";
	}

	@Secured("SUPER_ADMIN,ADMIN,OPERATEUR")
	@RequestMapping(method = RequestMethod.GET, value = "editservice")
	public String getEditFormService(@RequestParam int id, ModelMap model) {
		Service service = null;
		if (id != 0) {
			service = serviceService.getService(id);
		}
		if (service == null) {
			model.put("servicelist", serviceService.getServices());
			return "servicelist";
		}
		System.out.println(service.getId());
		model.addAttribute("service", service);
		return "formservice";
	}

	@RequestMapping(method = RequestMethod.GET, value = "servicelist")
	public String getServices(ModelMap model) {
		model.put("servicelist", serviceService.getServices());
		return "servicelist";
	}

	@Secured("SUPER_ADMIN,ADMIN")
	@RequestMapping(method = RequestMethod.POST, value = "newservice")
	public String addNewService(@ModelAttribute("service") Service service,
			BindingResult bindingResult,ModelMap model) {
		if (bindingResult.hasErrors()) {
			return "service";
		} else {
			serviceService.saveOrUpdateService(service);
			model.put("servicelist", serviceService.getServices());
			return "servicelist";
		}

	}

	@Secured("SUPER_ADMIN,ADMIN")
	@RequestMapping(method = RequestMethod.GET, value = "deleteservice")
	public String deleteService(@RequestParam("id") int id, ModelMap model) {
		Service service = null;
		if (id != 0) {
			service = serviceService.getService(id);
		}
		if (service != null) {
			serviceService.deleteService(service);
		}
		model.put("servicelist", serviceService.getServices());
		return "servicelist";
	}

	@Secured("CLIENT")
	@RequestMapping(method = RequestMethod.GET, value = "accesservice")
	public String getServices(@RequestParam String lien, ModelMap model) {
		model.put("lien", lien);
		return "accesservice";
	}

	@Secured("SUPER_ADMIN,ADMIN")
	@RequestMapping(method = RequestMethod.GET, value = "newparametreService")
	public String addNewParametre(@RequestParam("id") int id, ModelMap model) {
		
		Service service = null;
		model.put("id", id);
		if (id != 0) {
			service = serviceService.getService(id);
			model.put("parametreServiceList", service.getParametreService());
		}
		ParametreService parametreService = new ParametreService();
		model.put("parametreService", parametreService);
		return "parametreService";

	}

	@Secured("SUPER_ADMIN,ADMIN")
	@RequestMapping(method = RequestMethod.POST, value = "newparametreService")
	public String addNewParametre(
			@RequestParam("id") int id,
			@ModelAttribute("parametreService") ParametreService parametreService,
			BindingResult bindingResult, ModelMap model) {
		if (bindingResult.hasErrors()) {
			return "parametreService";
		} else {
			Service service = null;
			if (id != 0) {
				service = serviceService.getService(id);
				service.addParametreService(parametreService);
			}
			// parametreServiceDao.save(parametreService);
			parametreService.setDtCreation(new Date());

			// parametreService.getIdService().addParametreService(parametreService);
			serviceService.saveOrUpdateService(service);
			model.put("parametreServiceList", service.getParametreService());
			model.put("id", service.getId());
			parametreService = new ParametreService();
			model.put("parametreService", parametreService);
			return "parametreService";
		}

	}
	@Secured("SUPER_ADMIN,ADMIN")
	@RequestMapping(method = RequestMethod.GET, value = "deleteparametreservice")
	public String deleteParaService(@RequestParam("ids") int ids,
			@RequestParam("idp") long idp, ModelMap model) {
		ParametreService parametreservice = null;
		if (idp != 0) {
			parametreservice = parametreServiceDao.get(idp);
		}
		if (parametreservice != null) {
			parametreServiceDao.delete(parametreservice);
		}
		parametreservice = new ParametreService();
		Service service = null;
		model.put("id", ids);
		if (ids != 0) {
			service = serviceService.getService(ids);
			model.put("parametreServiceList", service.getParametreService());
		}
		model.put("parametreService", parametreservice);
		return "parametreService";
	}
}
